  package Messages;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.concurrent.Phaser;

import com.mysql.jdbc.ResultSet;

import Server.SQLConnection;
import ocsf.server.ConnectionToClient;
/**
 * This class implements the AddDoor to db
 *   @author Shlomit Yosfan
 * @author Nofar Sutliman
 */
public class MeassageAddDoor {

	private ConnectionToClient client ;
	private String[] str; 
	static int row1;
	
	/**
	 * Contractor of this class 
	 * @param str
	 * @param client
	 */
	
	public MeassageAddDoor(String[] str, ConnectionToClient client) {
		this.client= client ;
		this.str= str; 
	}

	/**
	 * Adding  Door to the DB	
	 * @throws SQLException
	 * @throws IOException
	 */
	
	public void AddDoor() throws SQLException, IOException{
		
		SQLConnection sc = new SQLConnection();
		
		java.sql.PreparedStatement doorId = sc.getConnection().prepareStatement("SELECT * FROM db.door;");
		java.sql.PreparedStatement doorInsert = sc.getConnection().prepareStatement("INSERT INTO db.door (DoorID,DoorType,Quantity,MinInventory,MaxInventory,SuppID,Measurement) VALUES(?,?,?,?,?,?,?);");
		
		
		ResultSet rs= (ResultSet) doorId.executeQuery();
		row1 = 0;
		while(rs.next()){
			int temp = rs.getInt(1);
				if(row1>temp)
					row1=row1;
					else{
					row1=temp;}
		}
			
		doorInsert.setInt(1, row1+1);					
		doorInsert.setString(2,str[5]);
		doorInsert.setString(3,str[4]);
		doorInsert.setString(4,str[1]);
		doorInsert.setString(5,str[2]);
		doorInsert.setString(6,str[3]);
		doorInsert.setString(7,str[6]);

		doorInsert.executeUpdate();
		doorInsert.close();
	
	
							ArrayList<Object> door=new ArrayList<Object>();
							door.add("AddDoor");		
							client.sendToClient(door);

	}
				
}
